package toubiao.utility;

import org.springframework.context.ApplicationContext;

import spring.SpringContextHolder;
import toubiao.dao.BaseDaoI;
import toubiao.dao.business.ContractDaoI;
import toubiao.dao.business.ProjectDaoI;
import toubiao.exception.CustomException;

public class ProApplicationContext {

	/** 
	* 取得存储在静态变量中的ApplicationContext. 
	*/  
	public static ApplicationContext getApplicationContext() {  
		return SpringContextHolder.getApplicationContext();  
	}  
	  
	/** 
	* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
	*/  
	@SuppressWarnings("unchecked")  
	public static <T> T getBean(String name) {  
		return (T) SpringContextHolder.getBean(name);  
	}  
	  
	/** 
	* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
	*/  
	@SuppressWarnings("unchecked")  
	public static <T> T getBean(Class<T> clazz) {  
		return (T) SpringContextHolder.getBean(clazz);  
	}  
	  
	
	
	    /**
	     * @throws CustomException 
	    * @Title: getDao
	    * @Description: 根据 className 返回 Dao
	    * @param @param clasName 如"project"
	    * @param @return    
	    * @return BaseDaoI<T>    
	    * @throws
	    */
	public static  BaseDaoI getDao(String className) throws CustomException {
		if(className.trim().equalsIgnoreCase("project")){
			return (ProjectDaoI) SpringContextHolder.getBean(ProjectDaoI.class);
		}
		
		if(className.trim().equalsIgnoreCase("contract")){
			return (ContractDaoI) SpringContextHolder.getBean(ContractDaoI.class);
		}
		
		throw new CustomException("className 无效，不在设置范围内");
	}
	

    /**
     * @throws CustomException 
    * @Title: getDao
    * @Description: 根据 className 返回 Dao
    * @param @param clasName 如"project"
    * @param @return    
    * @return BaseDaoI<T>    
    * @throws
    */
public static  <T> BaseDaoI<T> getDao(Class<T> clazz) throws CustomException {
	if(clazz.getSimpleName().equalsIgnoreCase("PoProject")){
		return (BaseDaoI<T>) SpringContextHolder.getBean(ProjectDaoI.class);
	}
	
	if(clazz.getSimpleName().equalsIgnoreCase("PoContract")){
		return (BaseDaoI<T>) SpringContextHolder.getBean(ContractDaoI.class);
	}
	
	throw new CustomException("className 无效，不在设置范围内");
}
	
}
